package com.newjiabo.ski.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.newjiabo.common.utils.DateUtils;
import com.newjiabo.common.utils.SnowFlake;
import com.newjiabo.common.utils.StringUtils;
import com.newjiabo.ski.domain.SkiMoreServices;
import com.newjiabo.ski.domain.SkiSetMealServicesRel;
import com.newjiabo.ski.domain.dto.SetMealServicesRelDto;
import com.newjiabo.ski.domain.enums.SetMealStatus;
import com.newjiabo.ski.domain.vo.SkiMetMealInfoVo;
import com.newjiabo.ski.service.ISkiMealSnowRelService;
import com.newjiabo.ski.service.ISkiMoreServicesService;
import com.newjiabo.ski.service.ISkiSetMealServicesRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.newjiabo.ski.mapper.SkiSetMealMapper;
import com.newjiabo.ski.domain.SkiSetMeal;
import com.newjiabo.ski.service.ISkiSetMealService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 套餐配置Service业务层处理
 *
 * @author CaoMengHui
 * @date 2023-09-18
 */
@Service
public class SkiSetMealServiceImpl implements ISkiSetMealService {
    @Resource
    private SkiSetMealMapper skiSetMealMapper;
    @Resource
    private ISkiMealSnowRelService mealSnowRelService;
    @Resource
    private ISkiSetMealServicesRelService skiSetMealServicesRelService;
    @Resource
    private ISkiMoreServicesService moreServicesService;

    /**
     * 查询套餐配置
     *
     * @param id 套餐配置主键
     * @return 套餐配置
     */
    @Override
    public SkiSetMeal selectSkiSetMealById(String id) {
        return skiSetMealMapper.selectSkiSetMealById(id);
    }

    /**
     * 查询套餐配置列表
     *
     * @param skiSetMeal 套餐配置
     * @return 套餐配置
     */
    @Override
    public List<SkiSetMeal> selectSkiSetMealList(SkiSetMeal skiSetMeal) {
        return skiSetMealMapper.selectSkiSetMealList(skiSetMeal);
    }

    /**
     * 新增套餐配置
     *
     * @param skiSetMeal 套餐配置
     * @return 结果
     */
    @Override
    public int insertSkiSetMeal(SkiSetMeal skiSetMeal) {
        if(StringUtils.isEmpty(skiSetMeal.getId())){
            skiSetMeal.setId(SnowFlake.getIdStr());
        }
        skiSetMeal.setCreateTime(DateUtils.getNowDate());
        skiSetMeal.setDelFlag(false);
        return skiSetMealMapper.insertSkiSetMeal(skiSetMeal);
    }

    /**
     * 修改套餐配置
     *
     * @param skiSetMeal 套餐配置
     * @return 结果
     */
    @Override
    public int updateSkiSetMeal(SkiSetMeal skiSetMeal) {
        skiSetMeal.setUpdateTime(DateUtils.getNowDate());
        return skiSetMealMapper.updateSkiSetMeal(skiSetMeal);
    }

    /**
     * 批量删除套餐配置
     *
     * @param ids 需要删除的套餐配置主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteSkiSetMealByIds(String[] ids) {
        // 删除套餐会取消 套餐与雪场之间的绑定关系
        mealSnowRelService.deleteSkiMealSnowRelBySetMealIds(ids);
        return skiSetMealMapper.deleteSkiSetMealByIds(ids);
    }

    /**
     * 删除套餐配置信息
     *
     * @param id 套餐配置主键
     * @return 结果
     */
    @Override
    public int deleteSkiSetMealById(String id) {
        return skiSetMealMapper.deleteSkiSetMealById(id);
    }

    @Override
    public List<SkiSetMeal> selectByIds(List<String> setMealIds) {
        return skiSetMealMapper.selectByIds(setMealIds);
    }

    @Override
    public List<SkiSetMeal> selectAll() {
        return skiSetMealMapper.selectAll();
    }

    @Override
    public int addSetMeal(SetMealServicesRelDto setMealServicesRelDto) {
        String setMealId = SnowFlake.getIdStr();
        this.initSetMeal(setMealServicesRelDto);
        this.initSetMealServicesRel(setMealServicesRelDto, setMealId);
        SkiSetMeal skiSetMeal = BeanUtil.copyProperties(setMealServicesRelDto, SkiSetMeal.class);
        skiSetMeal.setId(setMealId);
        return this.insertSkiSetMeal(skiSetMeal);
    }

    private void initSetMeal(SetMealServicesRelDto setMealServicesRelDto) {
        if (StringUtils.isEmpty(setMealServicesRelDto.getSetMealStatus())) {
            setMealServicesRelDto.setSetMealStatus(SetMealStatus.OPEN.name());
        }
    }

    @Override
    public int updateSetMeal(SetMealServicesRelDto setMealServicesRelDto, String setMealId) {
        this.initSetMealServicesRel(setMealServicesRelDto, setMealId);
        SkiSetMeal skiSetMeal = BeanUtil.copyProperties(setMealServicesRelDto, SkiSetMeal.class);
        skiSetMeal.setId(setMealId);
        return this.updateSkiSetMeal(skiSetMeal);
    }

    @Override
    public SkiMetMealInfoVo getInfo(String id) {
        List<SkiSetMealServicesRel> skiSetMealServicesRelList = skiSetMealServicesRelService.selectSkiSetMealServicesRelBySetMealId(id);
        List<SkiMoreServices> moreServicesList = new ArrayList<>();

        if(CollectionUtil.isNotEmpty(skiSetMealServicesRelList)){
            moreServicesList = moreServicesService.selectInIds(skiSetMealServicesRelList.stream().map(ski -> ski.getServicesId()).collect(Collectors.toList()));
        }
        SkiMetMealInfoVo skiMetMealInfoVo = BeanUtil.copyProperties(this.selectSkiSetMealById(id), SkiMetMealInfoVo.class);
        skiMetMealInfoVo.setSkiMoreServicesList(moreServicesList);
        return skiMetMealInfoVo;
    }

    @Override
    public List<SkiSetMeal> getBySnowFieldId(String snowFieldId, SetMealStatus setMealStatus) {
        return skiSetMealMapper.getBySnowIdAndStatus(snowFieldId,setMealStatus.name());
    }

    private void initSetMealServicesRel(SetMealServicesRelDto setMealServicesRelDto, String setMealId) {
        List<String> servicesIdList = setMealServicesRelDto.getServicesIdList();
        if (CollectionUtil.isNotEmpty(servicesIdList)) {
            List<SkiSetMealServicesRel> skiSetMealServicesRelList = new ArrayList<>();
            for (String servicesId : servicesIdList) {
                skiSetMealServicesRelList.add(new SkiSetMealServicesRel(setMealId, servicesId));
            }
            skiSetMealServicesRelService.bindingServices(skiSetMealServicesRelList);
        }
    }
}
